﻿using Sys.Core.Models.Dto;
using Sys.Core.Models.Queries;
using Sys.Core.Models.VO;
using MiniFox;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications;
using MiniFox.Platform.Applications.Auth;

namespace Sys.Core.Services
{
    public class OrgService : ApiServiceBase, IOrgService
    {
        public OrgService(SystemPrincipal principal) : base(principal)
        {
        }
        [AutoWired]
        public IOrgProvider Provider { get; set; }

        public ResultObject<IEnumerable<OrganizationVo>> Query(PagedQueryObject<OrgQuery> request)
        {
            return this.Provider.Query(request);
        }

        public OrganizationVo Item(string orgId)
        {
            return this.Provider.GetOrganization(orgId);
        }

        public OrganizationVo Create(OrganizationDto organization)
        {
            //organization.OrganizationID = SequenceGUID.NewGUID();
            if (!this.Provider.CreateOrganization(organization))
                return null;

            return this.Provider.GetOrganization(organization.OrganizationID);
        }

        public OrganizationVo Update(OrganizationDto organization)
        {
            if (!this.Provider.UpdateOrganization(organization))
                return null;

            return this.Provider.GetOrganization(organization.OrganizationID);
        }

        public string Delete(string orgId)
        {
            if (!this.Provider.DeleteOrganization(orgId))
                return null;

            return orgId;
        }

        public ITreeNode DeptTree(string orgId)
        {
            var depts = this.Provider.GetDepts(orgId);
            TreeGroup<DeptVo> treeGroup = new TreeGroup<DeptVo>(orgId, depts);
            return treeGroup.Tree;
        }
        protected void CreateDeptPath(DeptDto dept)
        {
            var parent = this.Provider.GetDept(dept.OrganizationID, dept.ParentDeptID);
            var deptPath = $"{parent.DeptPath}/{dept.DeptName.Trim()}";
            dept.DeptPath = deptPath;
        }
        protected void CheckDeptPath(string deptPath)
        {
            if (this.Provider.DeptExists(deptPath))
                throw new InvalidOperationException("部门已存在");
        }
        public DeptVo CreateDept(DeptDto dept)
        {
            this.CreateDeptPath(dept);
            this.CheckDeptPath(dept.DeptPath);
            dept.DeptID = SequenceGUID.NewGUID();
            return this.Provider.CreateDept(dept) ? this.Provider.GetDept(dept.DeptPath) : null;
        }

        public DeptVo UpdateDept(DeptDto dept)
        {
            this.CreateDeptPath(dept);
            return this.Provider.UpdateDept(dept) ? this.Provider.GetDept(dept.DeptPath) : null;
        }

        public string DeleteDept(string orgId, string deptId)
        {
            var dept = this.Provider.GetDept(orgId, deptId);
            var deptPath = dept.DeptPath;
            return this.Provider.DeleteDept(deptPath) ? deptPath : null;
        }

        public string DeleteDept(string deptPath)
        {
            return this.Provider.DeleteDept(deptPath) ? deptPath : null;
        }

        public void AddUsers(DeptUserSet deptUserSet)
        {
            this.Provider.AddDeptUsers(deptUserSet.DeptPath, deptUserSet.Users);
        }

        public void RemoveUsers(DeptUserSet deptUserSet)
        {
            this.Provider.RemoveDeptUsers(deptUserSet.DeptPath, deptUserSet.Users);
        }

        public ResultObject<IEnumerable<UserVo>> GetUsers(string orgId, string deptId, PagedQueryObject<UserQuery> query)
        {
            return this.Provider.GetUsers(orgId, deptId, query);
        }

        public ResultObject<IEnumerable<UserVo>> ExclusionUsers(string orgId, string deptId, PagedQueryObject<UserQuery> query)
        {
            return this.Provider.ExclusionUsers(orgId, deptId, query);
        }
    }
}